package com.sykj.bsc.jhy.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.sykj.bsc.jhy.entity.Bis_nurse_bag;
import com.sykj.bsc.jhy.entity.Bis_nurse_project;
import com.sykj.bsc.jhy.service.Bis_nurse_bagService;
import com.sykj.bsc.jhy.service.Bis_nurse_projectService;

@Controller
@RequestMapping("jhy")
public class Bis_nurse_bagController {

	@Autowired
	Bis_nurse_bagService impl;
	// 项目
	@Autowired
	Bis_nurse_projectService impl1;

	// 查询礼包列表
	@RequestMapping("bagList")
	public String selectBis_nurse_bag(Bis_nurse_bag bag, Model model) {
		List<Bis_nurse_bag> list = impl.selectBis_nurse_bag(bag);
		model.addAttribute("list", list);
		return "backstage/jhy/bagList";
	}

	// 删除礼包列表记录
	@RequestMapping("delBag/{bag_id}")
	public String delBis_nurse_bag(@PathVariable("bag_id") Integer bag_id) {
		impl.delBis_nurse_bag(bag_id);
		return "redirect:/jhy/bagList";
	}

	/**
	 * 选择项目
	 * 
	 * @param bag_id
	 * @return
	 */
	@RequestMapping("toadd")
	public String toadd(Model model, Bis_nurse_project bis_nurse_project) {
		List<Bis_nurse_project> list = impl1.selectBis_nurse_project(bis_nurse_project);
		model.addAttribute("list", list);
		model.addAttribute("tlist", impl1.selectBis_nurse_type());
		return "backstage/jhy/nurseProject1";
	}

	// 新增礼包页面
	@RequestMapping("addBag")
	public String addBag(HttpServletRequest request, HttpSession session) {
		String[] check = request.getParameterValues("ch");
		List<Bis_nurse_project> list = new ArrayList<>();
		if (check != null && !check.equals("")) {
			for (String str : check) {
				Bis_nurse_project nus = impl1.getBis_nurse_projectId(str);
				list.add(nus);
			}
		}
		session.setAttribute("list", list);
		return "backstage/jhy/addBis_nurse_bag";
	}

	// 新增礼包
	@RequestMapping("addtowne")
	public String add(HttpSession session, HttpServletRequest request, MultipartFile[] file,Bis_nurse_bag bnb,
			@RequestParam(value="file1")MultipartFile pictureFile1,@RequestParam(value="file2")MultipartFile pictureFile2) {
		//这个RequestParam(value="file")的是我们在jsp的name=“file”
		// 使用UUID给图片重命名，并去掉四个“-”
		String name = UUID.randomUUID().toString().replaceAll("-", "");
		// 获取文件的扩展名
		String ext1 = FilenameUtils.getExtension(pictureFile1
				.getOriginalFilename());
		String ext2 = FilenameUtils.getExtension(pictureFile2
				.getOriginalFilename());
		// 设置图片上传路径
		String url = request.getSession().getServletContext()
				.getRealPath("/js/upload");
		//System.out.println(url);
		// 以绝对路径保存重名命后的图片
		try {
			pictureFile1.transferTo(new File(url + "/" + name + "." + ext1));
			pictureFile2.transferTo(new File(url + "/" + name + "." + ext2));
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// 把图片存储路径保存到数据库
		bnb.setCover_big_photo("upload/" + name + "." + ext1);
		bnb.setCover_photo("upload/" + name + "." + ext2);
		
		List<Bis_nurse_project> sess = (List<Bis_nurse_project>) session.getAttribute("list");
		Map<String, Object> map = initone(request);

		map.put("project_new_price", request.getParameter("qq"));
		this.impl.savebis_nurse_bag(map);// 添加礼包
		for (Bis_nurse_project s : sess) {
			// 添加礼包与项目信息表
			System.out.println(s.getBeauty_price());
			map.put("project_old_price", s.getBeauty_price());
			map.put("project_id", s.getProject_id());
			this.impl.savebis_bag_project(map);
		}
		return "redirect:/jhy/bagList";
	}

	private Map<String, Object> initone(HttpServletRequest request) {
		// 生成至少4位随机数
		int max = 999999;
		int min = 1000;
		Random random = new Random();
		Integer ran = random.nextInt(max) % (max - min + 1) + min;
		Map<String, Object> map = new HashMap<>();
		map.put("bag_id", ran);//
		map.put("bag_name", request.getParameter("bag_name"));//
		map.put("status", request.getParameter("status"));//
		map.put("open_card_num", request.getParameter("aa"));//
		map.put("bag_total_price", request.getParameter("qq"));//
		map.put("remain_num", request.getParameter("aa"));//
		map.put("min_buy_num", request.getParameter("bb"));//
		map.put("max_buy_num", request.getParameter("cc"));//
		map.put("buy_count", request.getParameter("dd"));//
		map.put("overdue_time", request.getParameter("ee"));//
		map.put("putaway_time", request.getParameter("ff"));//
		map.put("soldout_time", request.getParameter("gg"));//
		map.put("sort_no", request.getParameter("kk"));//
		map.put("content", request.getParameter("jj"));//
		map.put("bag_introduce", request.getParameter("hh"));//
		return map;
	}

	@Autowired
	Bis_nurse_projectService bnpimpl;

	@RequestMapping("selectBagProject")
	public String selectBagProject(Bis_nurse_project bis_nurse_project, Model model) {
		List<Bis_nurse_project> bnp = bnpimpl.selectBis_nurse_project(bis_nurse_project);
		model.addAttribute("bnp", bnp);
		return "backstage/jhy/selectBagProject";
	}

	// 礼包详情
	@RequestMapping("selectBagById/{bag_id}")
	public String selectBagById(@PathVariable("bag_id") Integer bag_id, Model model) {
		Bis_nurse_bag bag = impl.selectBagById(bag_id);
		model.addAttribute("bag", bag);
		return "backstage/jhy/seeBis_nurse_bag";
	}

	// 修改礼包信息
	@RequestMapping("updateBag/{bag_id}")
	public String updateBag(@PathVariable("bag_id") Integer bag_id, Model model) {
		Bis_nurse_bag bnb = impl.selectBagById(bag_id);
		model.addAttribute("bnb", bnb);
		return "backstage/jhy/updateBis_nurse_bag";
	}
	
	@RequestMapping("updateBis_nurse_bag")
	public String updateBis_nurse_bag(Bis_nurse_bag bnb,HttpServletRequest request,
			@RequestParam(value="file1")MultipartFile pictureFile1,@RequestParam(value="file2")MultipartFile pictureFile2) {
		 //这个RequestParam(value="file")的是我们在jsp的name=“file”
			// 使用UUID给图片重命名，并去掉四个“-”
			String name = UUID.randomUUID().toString().replaceAll("-", "");
			// 获取文件的扩展名
			String ext1 = FilenameUtils.getExtension(pictureFile1
					.getOriginalFilename());
			String ext2 = FilenameUtils.getExtension(pictureFile2
					.getOriginalFilename());
			// 设置图片上传路径
			String url = request.getSession().getServletContext()
					.getRealPath("/js/upload");
			//System.out.println(url);
			// 以绝对路径保存重名命后的图片
			try {
				pictureFile1.transferTo(new File(url + "/" + name + "." + ext1));
				pictureFile2.transferTo(new File(url + "/" + name + "." + ext2));
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			// 把图片存储路径保存到数据库
			bnb.setCover_big_photo("upload/" + name + "." + ext1);
			bnb.setCover_photo("upload/" + name + "." + ext2);
		impl.updateBis_nurse_bag(bnb);
		return "redirect:/jhy/bagList";
	}

}
